package cn.yd.rb.controller;

import cn.yd.rb.po.User;
import cn.yd.rb.service.NewsService;
import cn.yd.rb.service.NovelService;
import cn.yd.rb.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private NovelService novelService;
    @Autowired
    private NewsService newsService;

    @RequestMapping("/regist")
    public String regist(User user) {
        userService.regist(user);
        System.out.println("注册成功！");
        return "loginAndRegist";
    }

    //返回登录页面的方法
    @RequestMapping("/toLogin")
    public String toLogin() {

        return "loginAndRegist";
    }

    //登录的方法
    @RequestMapping("/login")
    public String login(Model model, HttpSession session, User user) throws Exception {
        try {
            User userRes = userService.findUserByLogin(user);
            session.setAttribute("user", userRes);
            session.setAttribute("userVip", userRes.getIsVip());
        } catch (Exception e) {
            model.addAttribute("mess", e.getMessage());
            return "loginAndRegist";
        }
        return "redirect:/index";
    }

    //注销的方法
    @RequestMapping("/logout")
    public String login(HttpSession session) throws Exception {
        session.removeAttribute("user");
        session.removeAttribute("userVip");
        return "redirect:/index";
    }

    @RequestMapping("/getAllUsers")
    public String getAllUsers(Model model) {
        List<User> userList = userService.findAllUsers();
        model.addAttribute("userList", userList);
        return "admin/page/user/allUsers";
    }

    //忘记密码
    // 第一步：用户名校验
    @RequestMapping("/forget1")

    public String forget1(Model model, HttpSession session, User user) throws Exception {
        try {
            User userRes = userService.findUserByForget1(user);
            session.setAttribute("user", userRes);
        } catch (Exception e) {
            return "forget_1";
        }
        return "forget_2";
    }

    //忘记密码
    // 第二步：手机号校验
    @RequestMapping("/forget2")
    public String forget2(Model model, HttpSession session, User user) throws Exception {
        try {
            User userRef = userService.findUserByForget2(user);
        } catch (Exception e) {
            return "forget_2";
        }
        return "forget_3";
    }

    //忘记密码
    // 第三步：修改密码
    @RequestMapping("/forget3")
    public String forget3(User user) {
        try {
            userService.findUserByForget3(user);
        } catch (Exception E) {

        }
        return "forget_4";
    }

    //用户名重复校验
    @RequestMapping("unrepeat")
    public void check(String username, HttpServletResponse response) throws Exception {
        User userRes = userService.findUserByAjax(username);
        if(userRes != null){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("0");
        }
    }
    //成为会员
    @RequestMapping("/bevip")
    public String bevip(HttpSession session,User user) {
        try {
            User userRes = userService.findUserByBevip(user);
            session.setAttribute("user", userRes);
            session.setAttribute("userVip", userRes.getIsVip());
        } catch (Exception E) {
        }
        return "redirect:/index";
    }

}
